package hlhj.fhp.tvlib.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.widget.ImageView
import android.widget.Toast
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.RadioListBean
import kotlinx.android.synthetic.main.activity_broad_cast_aty.*
import java.util.ArrayList

class BroadCastAty : BaseAty() {
    private lateinit var adp:BaseQuickAdapter<RadioListBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<RadioListBean.DataBean>()
    private var page=1;
    private lateinit var loadingDialog:LoadingDialog
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView()
        supportActionBar!!.hide()

        loadingDialog= LoadingDialog(this)
        initRcy()
        getData()
        setSp()
        initListener()
    }

    override fun getContentId(): Int {
        return R.layout.activity_broad_cast_aty
    }

    private fun initRcy() {
        adp=object :BaseQuickAdapter<RadioListBean.DataBean,BaseViewHolder>(R.layout.broad_cast_item,datas){
            override fun convert(helper: BaseViewHolder?, item: RadioListBean.DataBean?) {
                val itemBg = helper?.getView<ImageView>(R.id.itemImg)
                Glide.with(mContext).load(Contents.IMG_URL+item?.radio_thumb).into(itemBg!!)
                helper?.setText(R.id.itemTittle,item?.radio_name)
                if (item?.online.equals("")||item?.online==null){
                    helper?.setVisible(R.id.itemNow,false)
                }else{
                    helper?.setVisible(R.id.itemNow,true)
                    helper?.setText(R.id.itemNow,"正在直播：${item?.online}")
                }
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        adp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this@BroadCastAty, CastAty::class.java)
            intent.putExtra("id",datas[position].id)
            intent.putExtra("tittle",datas[position].radio_name)
            intent.putExtra("now",datas[position].online)
            intent.putExtra("thumb",datas[position].radio_thumb)
            startActivity(intent)
        }
    }

    private fun setSp() {
        spView.header=DefaultHeader(this)
        spView.setListener(object :SpringView.OnFreshListener{
            override fun onLoadmore() {
            }

            override fun onRefresh() {
                datas.clear()
                getData()
            }
        })
    }

    private fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
    }

    private fun getData() {
        OkGo.get<String>(Contents.BaseUrl+Contents.RADIO_LIST)
                .params("page",page)
                .execute(object :StringCallback(){
                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                        loadingDialog.show()
                    }
                    override fun onSuccess(response: Response<String>?) {
                        loadingDialog.dismiss()
                        spView.onFinishFreshAndLoad()
                        val str = response?.body()
                        val listBean = Gson().fromJson(str, RadioListBean::class.java)
                        if (listBean.code==1){
                            datas.addAll(listBean.data)
                            adp.notifyDataSetChanged()
                        }else{
                            Toast.makeText(this@BroadCastAty,listBean.msg,Toast.LENGTH_SHORT).show()
                        }
                    }
                })
    }
}
